package com.rdzn.mall.message.controller;

import com.aaron.springcloud.wx.AbstractMessageCallBackController;
import com.aaron.springcloud.wx.handler.MessageHandlerAdapterContext;
import com.aaron.springcloud.wx.handler.MessageHandlerContext;
import com.aaron.springcloud.wx.register.WxCallBackTypeEnum;
import com.alibaba.fastjson.JSON;
import com.rdzn.mall.message.hander.WelcomeHandler;
import com.rdzn.mall.message.service.IWxMinipService;
import com.rdzn.mall.template.Template;
import com.rdzn.mall.template.TemplatePaySuccess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rdzn.mall.base.common.Result;

/**
 * @author zhigangding
 * @date 2020/2/25
 */
@Slf4j
@Controller
@Api(tags = "MessageController", description = "订阅消息推送")
@RequestMapping("/wx")
public class WXMessageController extends AbstractMessageCallBackController {

    @Autowired
    private IWxMinipService wxMinipService;

    public static final String appId = "wx6fa7afdded18bd22";

    @ApiOperation(value = "发送微信订阅消息")
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> send(@RequestBody Template<TemplatePaySuccess> template) {
        log.info("message send params : {}", JSON.toJSONString(template));
        return Result.ok(wxMinipService.send(template));
    }


    @Override
    protected void registerHandler(MessageHandlerContext handlerContext) {
        //申明一个MessageHandlerAdapterContext，对应一个appId下的事件处理，同一个appId可以注册多个MessageHandlerAdapterContext
        MessageHandlerAdapterContext context = new MessageHandlerAdapterContext(appId);
        WelcomeHandler handler = new WelcomeHandler();
        //这里是注册一个事件对应的处理器
        context.addMessageHandlerAdapter(WxCallBackTypeEnum.SEND_TEXT_MESSAGE, handler);

        //这里是注册一个小程序或者公众号对应的处理器
        handlerContext.addMessageHandler(context);
    }
}
